const app = getApp();
const {
  AskPage
} = app._require("common/index");
const formMixin = app._require("mixins/form.mixin.js").default;
AskPage({
  /**
   * 页面的初始数据
   */
  name: "changePassword",
  mixins: [formMixin],
  data: {
    statusBar: app.globalData.StatusBar,
    customBar: app.globalData.CustomBar,
    defaultPsd: "",
    newPassShow: false,
    confirmPassShow: false,
    errorPsd:"",
    errorPsdNew:"",
    errorPsdConfirm:"",
    model: {
      password: "",
      newPsd: "",
      confirmPsd: ""
    }

  },

  onInput(e) {
    const value = e.detail.value;
    this.setModel({
      currentTarget: e.currentTarget,
      detail: {
        value,
      },
    });
  },

  // checkPsd() {
  //   let errorPsd = this.data.errorPsd
  //   if(!this.data.model.password){
  //     errorPsd = '请输入密码';
  //   }else if (this.data.model.password != this.data.defaultPsd) {
  //     errorPsd = '密码错误';
  //     console.log( this.errorPsd);
  //   } else {
  //     errorPsd = '';
  //   }
  //   this.setData({errorPsd})
  // },



  checkPsdNew(){
    let errorPsdNew = this.data.errorPsdNew
    if((this.data.model.newPsd!=this.data.model.confirmPsd)&&(this.data.model.newPsd!='')&&(this.data.model.confirmPsd!='')){
      errorPsdNew='两次密码不同'
    }else{
      errorPsdNew=''
    }
    this.setData({errorPsdNew})

    let errorPsdConfirm = this.data.errorPsdConfirm
    if((this.data.model.newPsd!=this.data.model.confirmPsd)&&(this.data.model.newPsd!='')&&(this.data.model.confirmPsd!='')){
      errorPsdConfirm='两次密码不同'
    }else{
      errorPsdConfirm=''
    }
    this.setData({errorPsdConfirm})

  },
  checkPsdConfirm(){
    let errorPsdNew = this.data.errorPsdNew
    if((this.data.model.newPsd!=this.data.model.confirmPsd)&&(this.data.model.newPsd!='')&&(this.data.model.confirmPsd!='')){
      errorPsdNew='两次密码不同'
      console.log('两次密码不同');
    }else{
      errorPsdNew=''
    }
    this.setData({errorPsdNew})

    let errorPsdConfirm = this.data.errorPsdConfirm
    if((this.data.model.newPsd!=this.data.model.confirmPsd)&&(this.data.model.newPsd!='')&&(this.data.model.confirmPsd!='')){
      errorPsdConfirm='两次密码不同'
      console.log('两次密码不同');
    }else{
      errorPsdConfirm=''
    }
    this.setData({errorPsdConfirm})
  },

  async save() {
    let {
      password,
      newPsd,
      confirmPsd
    } = this.data.model;
    let res = this.validateFiled([
      [password, {
        required: "请输入原密码!"
      }],
      [newPsd, {
        required: "请输入新密码!"
      }],
      [confirmPsd, {
        required: "请确认新密码!"
      }],
    ]);

    console.log(newPsd);
    console.log(confirmPsd);

    if (!res.result) {
      wx.showToast({
        title: res.msg,
        icon: "none",
      });
      return false;
    }
    if(newPsd!=confirmPsd){
      wx.showToast({
        title: '两次密码不同',
        icon: "none",
      });
      return false;
    }
    var reg=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
        if(!reg.test(newPsd)||newPsd.length<6||newPsd.length>12){
          wx.showToast({
            title: '密码格式应该限制为6-12长度的数字和字母组合',
            icon: "none",
          });
          return false;
        }
        if(!reg.test(confirmPsd)||confirmPsd.length<6||confirmPsd.length>12){
          wx.showToast({
            title: '密码格式应该限制为6-12长度的数字和字母组合',
            icon: "none",
          });
          return false;
        }
    try{
      const response = await this.$_server("user/self", {
        data: {
          password: password,
          newPassword: newPsd,
        },
    });
    let { data } = response;
    if (data.code !== 200) {
        throw data;
    }
    console.log('---');
    console.log(response);
    wx.navigateBack({
      delta: 1
    })
    }catch (error) {
      console.log('===');
      console.log(error);
      // statements
      if (error.message) {
          wx.showToast({
              mask: true,
              title: error.message,
              icon: "none",
              duration: 2000,
          });
      } else {
          throw error;
      }
  }
  },

  changeComeBack(){
    console.log("11111")
    wx.navigateBack({

    })
  },



  onIconClickNew() {
    this.setData({
      newPassShow: !this.data.newPassShow,
    });
  },
  onIconClickConfirm() {
    this.setData({
      confirmPassShow: !this.data.confirmPassShow,
    });
  },
  clickShow(e){
    console.log(e.target.dataset.type)
    let type  = e.target.dataset.type;
    if(type=="one"){
      this.setData({
        newPassShow: !this.data.newPassShow,
        // confirmPassShow: false,
      })
    }
    else{
      this.setData({
        confirmPassShow: !this.data.confirmPassShow,
      })
    }
  },
 
});